//
//  XHRemotePushManagar.m
//  ShowYork
//
//  Created by hxh on 2020/11/30.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "XHRemotePushManagar.h"
#import "TabBarViewController.h"
#import "CampusVideoPlayerViewController.h"
#import "NavigationViewController.h"
#import "VideoPlayerViewController.h"
#import "MineAboutListViewController.h"
#import "MineCommentListViewController.h"
#import "MineDoLikeListViewController.h"
#import "CommonWebViewController.h"
#import "NewsListViewController.h"

#import "AppDelegate.h"
@implementation XHRemotePushManagar
SingletonM(XHRemotePushManagar);

- (void)showRemotePushCustomData:(NSDictionary *)data{
    UIViewController *vc = [RPAppManager jsd_findVisibleViewController];
          
    if ([vc isKindOfClass:[VideoPlayerViewController class]]) {
        [((VideoPlayerViewController *)vc) stopVideoPlayer];
    }
    if ([vc isKindOfClass:[CampusVideoPlayerViewController class]]) {
        // 关闭播放并且切换屏幕
        [((CampusVideoPlayerViewController *)vc) stopVideoPlayer];
    }
    
    NSString *type = data[@"type"];
    NSString *url = data[@"url"];
    if (type != nil && ([type isEqualToString:@"1"] || ([type isEqualToString:@"h5"] && ![url containsString:@"/h5/lesson/news"]))) {
        CommonWebViewController *webVC = [[CommonWebViewController alloc]init];
        // 判断url是否为空
        if (data[@"url"] != nil && [data[@"url"] isKindOfClass:[NSString class]]) {
            webVC.webUrl = data[@"url"];
            webVC.isDark = YES;
            [vc.navigationController pushViewController:webVC animated:YES];
        }
    }else if (type != nil && ([type isEqualToString:@"2"] || ([type isEqualToString:@"local"] && [url containsString:@"local://ddup.app/main"]))){
        // 统一处理modal页面的逻辑防止页面不跳转
//        UIViewController *modalVC = vc;
//        while(modalVC.presentingViewController != nil){
//            modalVC = modalVC.presentingViewController;
//        }
//        [modalVC dismissViewControllerAnimated:NO completion:nil];
//        // 切回tabbar索引1的位置
//        AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//        UIViewController * controller = appDelegate.window.rootViewController;
//        TabBarViewController * tabBarController = (TabBarViewController *)controller;
//        [tabBarController setSelectedIndex:1];
//        [vc.navigationController popToRootViewControllerAnimated:YES];
//        // 相关modal页面回到根页面  部分页面跳转 push-modal-push
//        [[NSNotificationCenter defaultCenter]postNotificationName:@"popToRootViewControllerByRemoteMessage" object:nil];
    }else if (type != nil && ([type isEqualToString:@"3"] || ([type isEqualToString:@"local"] && [url containsString:@"local://ddup.app/parentKnow"]))){
        NewsListViewController *listVC = [[NewsListViewController alloc] init];
        [vc.navigationController pushViewController:listVC animated:YES];
    }else if (type != nil && ([type isEqualToString:@"4"] || ([type isEqualToString:@"h5"] && [url containsString:@"/h5/lesson/news"]))){
        if (data[@"url"] != nil && [data[@"url"] isKindOfClass:[NSString class]]) {
            NSString *url = data[@"url"];
            NSRange range = [url rangeOfString:@"?"];
            if (range.location == NSNotFound) {
                if ([url containsString:@"isApp=true"]) {
                    url = [NSString stringWithFormat:@"%@?userId=%@",url,[RPAppManager getCustomerId]];
                }else{
                    url = [NSString stringWithFormat:@"%@?isApp=true&userId=%@",url,[RPAppManager getCustomerId]];
                }
            }else{
                if ([url containsString:@"isApp=true"]) {
                    url = [NSString stringWithFormat:@"%@&userId=%@",url,[RPAppManager getCustomerId]];
                }else{
                    url = [NSString stringWithFormat:@"%@&isApp=true&userId=%@",url,[RPAppManager getCustomerId]];
                }
            }
            CommonWebViewController *newsDtlVC = [[CommonWebViewController alloc]init];
            newsDtlVC.webUrl = url;
            newsDtlVC.isDark = YES;
            [vc.navigationController pushViewController:newsDtlVC animated:YES];
        }
    }else{
     
    }
}

- (void)showRemotePushVCWithData:(MineMessageItemModel *)data{
   
    UIViewController *vc = [self jsd_findVisibleViewController];

    // 视频相关联需要先退出全屏状态
    if ([vc isKindOfClass:[VideoPlayerViewController class]] || [vc isKindOfClass:[CampusVideoPlayerViewController class]]) {
        // 停止播放 退出全屏
        if ([vc isKindOfClass:[VideoPlayerViewController class]]) {
            [((VideoPlayerViewController *)vc) stopVideoPlayer];
        }else{
            [((CampusVideoPlayerViewController *)vc) stopVideoPlayer];
        }
        // 点赞
        if (data.messageType.intValue == 1) {
            MineDoLikeListViewController *doLikeListVC = [[MineDoLikeListViewController alloc]init];
            [vc.navigationController pushViewController:doLikeListVC animated:YES];
        // @我的
        }else if (data.messageType.intValue == 2) {
            MineAboutListViewController *aboutListVC = [[MineAboutListViewController alloc]init];
            [vc.navigationController pushViewController:aboutListVC animated:YES];
        // 评论或者回复
        }else if(data.messageType.intValue == 3 || data.messageType.intValue == 4){
            MineCommentListViewController *commentListVC = [[MineCommentListViewController alloc]init];
            [vc.navigationController pushViewController:commentListVC animated:YES];
        }else{
          
        }
    }else{
        // 点赞
        if (data.messageType.intValue == 1) {
            if ([vc isKindOfClass:[MineDoLikeListViewController class]]) {
                [((MineDoLikeListViewController *)vc) requestData];
            }else{
                MineDoLikeListViewController *doLikeListVC = [[MineDoLikeListViewController alloc]init];
                [vc.navigationController pushViewController:doLikeListVC animated:YES];
            }
        // @我的
        }else if (data.messageType.intValue == 2) {
            if ([vc isKindOfClass:[MineAboutListViewController class]]) {
                [((MineDoLikeListViewController *)vc) requestData];
            }else{
                MineAboutListViewController *aboutListVC = [[MineAboutListViewController alloc]init];
                [vc.navigationController pushViewController:aboutListVC animated:YES];
            }
        // 评论或者回复
        }else if(data.messageType.intValue == 3 || data.messageType.intValue == 4){
            if ([vc isKindOfClass:[MineCommentListViewController class]]) {
                [((MineCommentListViewController *)vc) requestData];
            }else{
                MineCommentListViewController *commentListVC = [[MineCommentListViewController alloc]init];
                [vc.navigationController pushViewController:commentListVC animated:YES];
               
            }
        }else{
        
        }
    }
}




- (UIViewController *)jsd_findVisibleViewController {
    
    UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
    NSAssert(window, @"The window is empty");
    UIViewController* currentViewController = window.rootViewController;

    BOOL runLoopFind = YES;
    while (runLoopFind) {
        if (currentViewController.presentedViewController) {
            currentViewController = currentViewController.presentedViewController;
        } else {
            if ([currentViewController isKindOfClass:[UINavigationController class]]) {
                currentViewController = ((UINavigationController *)currentViewController).visibleViewController;
            } else if ([currentViewController isKindOfClass:[UITabBarController class]]) {
                currentViewController = ((UITabBarController* )currentViewController).selectedViewController;
            } else {
                break;
            }
        }
    }
    
    return currentViewController;
}


@end
